package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.Channel;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-7-27.
 */
@Repository
public class ChannelDaoImpl extends BaseDaoImpl<Channel> {

    /**
     * 获取详情
     *
     * @param unitId       单位id
     * @param channelShort 序号
     * @return
     */
    public Map<String, Object> getChannelByShort(int unitId, int channelShort) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT " +
                " ti.*" +
                " FROM " +
                " t_channel ti " +
                " WHERE " +
                " ti.unit_id = ? and channel_short = ? ");
        arrList.add(unitId);
        arrList.add(channelShort);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 获取详情
     *
     * @param unitId       单位id
     * @param channelId 序号
     * @return
     */
    public Map<String, Object> getChannelById(int unitId, int channelId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT ti.* FROM t_channel ti WHERE ti.unit_id = ? and id = ? ");
        arrList.add(unitId);
        arrList.add(channelId);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 更新
     *
     * @param info 频道内容
     * @return
     */
    public void update(Channel info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_channel set update_time = now(),updater = ?");
        arrList.add(info.getUpdater());
        sb.append(",channel_type = ? ");
        arrList.add(info.getChannel_type());
        sb.append(",channel_name = ? ");
        arrList.add(info.getChannel_name());
        sb.append(",share_title = ? ");
        arrList.add(info.getShare_title());
        sb.append(",share_desc = ? ");
        arrList.add(info.getShare_desc());

        sb.append(",channel_img_url = ? ");
        arrList.add(info.getChannel_img_url());
        sb.append(",uncheck_channel_img_url = ? ");
        arrList.add(info.getUncheck_channel_img_url());
        sb.append(",check_color = ? ");
        arrList.add(info.getCheck_color());
        sb.append(",uncheck_color = ? ");
        arrList.add(info.getUncheck_color());

        sb.append(",is_banner = ? ");
        arrList.add(info.getIs_banner());
        sb.append(",show_channel = ? ");
        arrList.add(info.getShow_channel());

        sb.append(",is_read = ? ");
        arrList.add(info.getIs_read());
        sb.append(",is_label = ? ");
        arrList.add(info.getIs_label());
        sb.append(" where id = ? and unit_id = ?");
        arrList.add(info.getId());
        arrList.add(info.getUnit_id());
        updateJdbc(sb.toString(), arrList);
    }


    public List<Channel> getNewsChannelList(int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT " +
                " ti.*" +
                " FROM " +
                " t_channel ti " +
                " WHERE " +
                " ti.unit_id = ? and channel_type = 1 ");
        arrList.add(unitId);
        List<Channel> re = findList(sb.toString(), arrList);
        return re;
    }

    /**
     * 根据类型查询
     * @param unitId
     * @param channelType
     * @return
     */
    public List<Channel> getChannelList(int unitId, int channelType){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT ti.* FROM t_channel ti WHERE ti.unit_id = ? and channel_type = ? and show_channel = 1");
        arrList.add(unitId);
        arrList.add(channelType);
        return findList(sb.toString(), arrList);
    }


    public List<Channel> getAllChannelList(int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT " +
                " ti.*" +
                " FROM " +
                " t_channel ti " +
                " WHERE " +
                " ti.unit_id = ?  ");
        arrList.add(unitId);
        List<Channel> re = findList(sb.toString(), arrList);
        return re;
    }

    public void settingColor(Channel info){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_channel set check_color=?,uncheck_color=? where unit_id = ? ");
        arrList.add(info.getCheck_color());
        arrList.add(info.getUncheck_color());
        arrList.add(info.getUnit_id());
        updateJdbc(sb.toString(), arrList);
    }

}
